/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hookedonplay.decoviewsample;

import com.hookedonplay.decoviewlib.DecoView;
import com.hookedonplay.decoviewlib.PointF;
import com.hookedonplay.decoviewlib.charts.ChartSeries;
import com.hookedonplay.decoviewlib.charts.SeriesItem;
import com.hookedonplay.decoviewlib.events.DecoEvent;
import com.hookedonplay.decoviewlib.interpolator.LinearInterpolator;
import com.hookedonplay.decoviewsample.ResourceTable;
import com.hookedonplay.decoviewsample.Utility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

public class SamplePauseAbility extends Ability {
    private int mSeries1Index;
    private int mPieIndex;

    private DecoView decoView;
    private Text textPause;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_sample_pause);

        decoView = (DecoView) findComponentById(ResourceTable.Id_decoView);
        textPause = (Text) findComponentById(ResourceTable.Id_textProgress);

        createTracks();
    }

    private void createTracks() {
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(64, 96, 214));
        decoView.setBackground(element);

        decoView.executeReset();
        decoView.deleteAll();

        final float seriesMax = 100f;

        float circleInset = Utility.getDimension(18, getApplicationContext());
        SeriesItem seriesBack1Item = new SeriesItem.Builder(RgbPalette.parse("#11000000"))
                .setRange(0, seriesMax, 0)
                .setChartStyle(SeriesItem.ChartStyle.STYLE_PIE)
                .setInset(new PointF(circleInset, circleInset))
                .build();

        mPieIndex = decoView.addSeries(seriesBack1Item);

        SeriesItem series1Item = new SeriesItem.Builder(RgbPalette.parse("#FFFFC107"))
                .setRange(0, seriesMax, 0)
                .setLineWidth(Utility.getDimension(36, getApplicationContext()))
                .setInterpolator(new LinearInterpolator())
                .build();

        series1Item.addArcSeriesItemListener(new SeriesItem.SeriesItemListener() {
            @Override
            public void onSeriesItemAnimationProgress(float percentComplete, float currentPosition) {
                decoView.getChartSeries(mPieIndex).setPosition(percentComplete < 1.0f ? percentComplete * seriesMax : 0f);
            }

            @Override
            public void onSeriesItemDisplayProgress(float percentComplete) {

            }
        });

        mSeries1Index = decoView.addSeries(series1Item);

        final DecoEvent.ExecuteEventListener eventListener = new DecoEvent.ExecuteEventListener() {
            @Override
            public void onEventStart(DecoEvent event) {
                textPause.setText("PAUSE");
            }

            @Override
            public void onEventEnd(DecoEvent event) {
                textPause.setText("");
                decoView.getChartSeries(mPieIndex).reset();
            }
        };

        Button buttonEmpty = (Button) findComponentById(ResourceTable.Id_buttonEmpty);
        buttonEmpty.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                decoView.addEvent(new DecoEvent.Builder(0)
                        .setIndex(mSeries1Index)
                        .setListener(eventListener)
                        .build());
            }
        });

        Button buttonFifty = (Button) findComponentById(ResourceTable.Id_buttonFifty);
        buttonFifty.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                decoView.addEvent(new DecoEvent.Builder(seriesMax * 0.5f)
                        .setIndex(mSeries1Index)
                        .setListener(eventListener)
                        .build());
            }
        });

        Button buttonFull = (Button) findComponentById(ResourceTable.Id_buttonFull);
        buttonFull.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                decoView.addEvent(new DecoEvent.Builder(seriesMax)
                        .setIndex(mSeries1Index)
                        .setListener(eventListener)
                        .build());
            }
        });

        textPause.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ChartSeries series = decoView.getChartSeries(mSeries1Index);
                if (series.isPaused()) {
                    series.resume();
                } else {
                    if (series.pause()) {
                        textPause.setText("RESUME");
                        decoView.getChartSeries(mPieIndex).reset();
                    }
                }
            }
        });
    }
}